package com.rongkang.interview.juc.blockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * @Description:
 * @author: 荣康
 * @date: 2021/12/11
 *
 *不存放数据的队列
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"\t  put A");
            try { synchronousQueue.put("A"); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println(Thread.currentThread().getName()+"\t  put B");
            try { synchronousQueue.put("B"); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println(Thread.currentThread().getName()+"\t  put C");
            try { synchronousQueue.put("C"); } catch (InterruptedException e) { e.printStackTrace(); }
        },"t1").start();

        new Thread(()->{
            //第一次取
            System.out.println(Thread.currentThread().getName()+"\t take A");
            try { synchronousQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); }
            //第二次取
            System.out.println(Thread.currentThread().getName()+"\t take B");
            try { synchronousQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); }
            //第三次取
            System.out.println(Thread.currentThread().getName()+"\t take C");
            try { synchronousQueue.take(); } catch (InterruptedException e) { e.printStackTrace(); }
        },"t2").start();
    }
}
